#!/bin/sh

function convertDir() {
    mkdir "$1"  #！创建文件夹
    for file in ./*
	do
	if test -f "$file"
	then
	    if [ "${file##*.}" = "xsd" ]
	    then
            echo "${file##*\/}"  #!打印出被转换的xsd文件名
	    	"${0%/*}"/SchemaToBean "${PWD}"/"${file##*/}" -out="$1" #!使用SchemaToBean小程序进行转换
            echo "#import \"${file##*\/}\"" >> "${0%/*}"/beans/Model.xsd #!将#import “xx.h”写入./beans/Model..xsd文件中
	    fi
	fi
	if test -d "$file"
	then
	    cd "$file"
	    convertDir "$1"/"${file##*\/}"
	    cd ..
	fi
	done
}

cd "${0%/*}"
rm -drf ./beans
mkdir ./beans
touch ./beans/Model.xsd
echo "#ifndef SalesSystem_Model_h\n#define SalesSystem_Model_h\n" >> "${0%/*}"/beans/Model.xsd  #!写入预编译头

cd ./source
convertDir "${0%/*}"/beans

echo "\n#endif" >> "${0%/*}"/beans/Model.xsd    #!写入预编译尾
sed 's:.xsd:.h:g' "${0%/*}"/beans/Model.xsd > "${0%/*}"/beans/Model.h   #!将.xsd字符串替换为.h
rm "${0%/*}"/beans/Model.xsd   #删除Model.xsd文件

echo success




